package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Car;
import com.shaoshaohuo.app.entity.Cargo;
import com.shaoshaohuo.app.ui.fragment.CarFragment;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.ImageLoadUtil;
import com.shaoshaohuo.app.utils.Location2Util;
import com.shaoshaohuo.app.utils.StringUtil;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by cao on 2016/1/6.
 */
public class HeaderListAdapter extends BaseAdapter {

    private String lat;
    private String lng;

    private Context context;
    private List<Cargo> goodsList;
    private List<Car> carList;
    private View view;
    private int listType = CarFragment.GOODS;


    public HeaderListAdapter(Context context,List<Cargo> goodsList,List<Car> carList,View view, String lat, String lng){
        this.context = context;
        this.goodsList = goodsList;
        this.carList = carList;
        this.view = view;
        this.lat = lat;
        this.lng = lng;
    }

    @Override
    public int getCount() {
        return listType==CarFragment.GOODS?goodsList.size()+1:carList.size()+1;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        }else {
            if (listType == CarFragment.GOODS) {
                return 1;
            }else{
                return 2;
            }
        }
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case 0:
                return view;
            case 1:
                position = position - 1;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_goods,null);
                }

                GoodsViewHolder goodsHolder = GoodsViewHolder.getHolder(convertView);
                Cargo goods = goodsList.get(position);

                goodsHolder.tv_name.setText(goods.getOrdername());
                if (TextUtils.isEmpty(goods.getShippweight())) {
                    goodsHolder.tv_num.setText(goods.getShipparea()+"立方米");
                }else{
                    goodsHolder.tv_num.setText(goods.getShippweight()+"千克");
                }
                goodsHolder.tv_start_time.setText(goods.getOrigintime());
                goodsHolder.tv_start_ad.setText(AreaUtil.getAreaFullname(goods.getOrigincityid()) + goods.getOriginaddress());
                goodsHolder.tv_end_ad.setText(AreaUtil.getAreaFullname(goods.getReceivecityid()) + goods.getReceiveaddress());
                goodsHolder.tv_end_time.setText(goods.getReceivetime());
                if (StringUtil.isNumericEmpty(goods.getOriginlat()) || StringUtil.isNumericEmpty(goods.getOriginlng())
                        || StringUtil.isNumericEmpty(goods.getReceivelat()) || StringUtil.isNumericEmpty(goods.getReceivelng())) {
                    goodsHolder.tv_long.setText("- -");
                } else {
                    goodsHolder.tv_long.setText(Location2Util.getInstance(context).distance(goods.getOriginlat(),
                            goods.getOriginlng(), goods.getReceivelat(), goods.getReceivelng()) + "km");
                }
                break;
            case 2:
                position = position - 1;
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_car, null);
                }

                CarViewHolder carHolder = CarViewHolder.getHolder(convertView);
                Car car = carList.get(position);

                carHolder.tv_name.setText(car.getCaruserinfo().getRealname());
                carHolder.tv_num.setText(car.getCarnum());
                if ("2".equals(car.getTriptype())) {
                    carHolder.tv_wangfan.setVisibility(View.VISIBLE);
                    carHolder.tv_zhifa.setVisibility(View.GONE);
                }else{
                    carHolder.tv_wangfan.setVisibility(View.GONE);
                    carHolder.tv_zhifa.setVisibility(View.VISIBLE);
                }
                ImageLoader.getInstance().displayImage(car.getCarphoto1(), carHolder.iv_picture, ImageLoadUtil.build);
                carHolder.tv_car_type.setText(car.getCarcname());
                carHolder.tv_car_long.setText(formatDouble(car.getCarlong())+"m");
                carHolder.tv_time.setText(car.getUtime());
                String toLat = car.getLat();
                String toLng = car.getLng();
                if (StringUtil.isNumericEmpty(toLat) || StringUtil.isNumericEmpty(toLng)
                        || StringUtil.isNumericEmpty(lat) || StringUtil.isNumericEmpty(lng)) {
                    carHolder.tv_car_juli.setText("- -");
                } else {
                    carHolder.tv_car_juli.setText(Location2Util.getInstance(context).distance(lat, lng, toLat, toLng) + "km");
                }
                /*if ("-1".equals(car.getDistance()) || "-1.0".equals(car.getDistance())) {
                    carHolder.tv_car_juli.setText("未知");
                }else{
                    carHolder.tv_car_juli.setText((formatDouble(car.getDistance())) + "km");
                }*/
                carHolder.tv_car_ad.setText(car.getAddress());
                break;
        }

        return convertView;
    }


    public void setCarList(List<Car> carList) {
        this.carList = carList;
    }

    public void notifyData(int listType){
        this.listType = listType;
        notifyDataSetChanged();
    }

    static class GoodsViewHolder{

        TextView tv_long;
        TextView tv_end_time;
        TextView tv_end_ad;
        TextView tv_start_ad;
        TextView tv_start_time;
        TextView tv_name;
        TextView tv_num;

        public GoodsViewHolder(View convertView) {
            tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            tv_start_time = (TextView) convertView.findViewById(R.id.tv_start_time);
            tv_start_ad = (TextView) convertView.findViewById(R.id.tv_start_ad);
            tv_end_ad = (TextView) convertView.findViewById(R.id.tv_end_ad);
            tv_end_time = (TextView) convertView.findViewById(R.id.tv_end_time);
            tv_long = (TextView) convertView.findViewById(R.id.tv_long);
            convertView.setTag(this);
        }

        public static GoodsViewHolder getHolder(View convertView){
            return (GoodsViewHolder) (convertView.getTag() != null ? convertView.getTag() : new GoodsViewHolder(convertView));
        }
    }

    static class CarViewHolder{

        public TextView tv_name;
        public  TextView tv_num;
        public TextView tv_wangfan;
        public TextView tv_zhifa;
        public ImageView iv_picture;
        public TextView tv_car_type;
        public  TextView tv_car_long;
        public   TextView tv_time;
        public   TextView tv_car_juli;
        public   TextView tv_car_ad;

        public CarViewHolder(View convertView) {
            tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            tv_wangfan = (TextView) convertView.findViewById(R.id.tv_wangfan);
            tv_zhifa = (TextView) convertView.findViewById(R.id.tv_zhifa);
            iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tv_car_type = (TextView) convertView.findViewById(R.id.tv_car_type);
            tv_car_long = (TextView) convertView.findViewById(R.id.tv_car_long);
            tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            tv_car_juli = (TextView) convertView.findViewById(R.id.tv_car_juli);
            tv_car_ad = (TextView) convertView.findViewById(R.id.tv_car_ad);
            convertView.setTag(this);
        }

        public static CarViewHolder getHolder(View convertView){
            return (CarViewHolder) (convertView.getTag() != null ? convertView.getTag() : new CarViewHolder(convertView));
        }
    }


    public static String formatDouble(String text) {

        String format;
        try {
            DecimalFormat df = new DecimalFormat("0.0");
            double d = Double.parseDouble(text)/1000;
            format = df.format(d);
            return format;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return "0";
    }
}
